import cloud from '@lafjs/cloud'
import { baiwangInvoice } from '@/uni/invoice/baiwangInvoiceUtils'
let dbName = "invoicing"
let ctxBody

export default async function (ctx: FunctionContext) {
  ctxBody = ctx
  let ret = await cloud.nw.db.findByWhereJson({
    dbName,
    whereJson: { ddlsh: ctxBody.body.data.transaction_id },
  });
  if (ret && ret.status == 2) return cloud.utils.requestReturn({ code: 500, msg: "该订单已开过票" })
  switch (ctxBody.body.type) {
    case "add":  //新开票
      return addInvoice()
      break;
    case "again":  //重新开票
      return againInvoice()
      break;
    default:
      break;
  }
}

let addInvoice = async () => {
  let info = ctxBody.body.data

  const { data } = await cloud.invoke("uni/invoice/queryUniqueSign_noauth", { ...ctxBody })
  //console.log("开票方信息", data);

  const json_data = {
    qyId: data.qyId,
    ddlsh: info.transaction_id,
    fplxdm: "02",
    kplx: "0",
    tspz: "00",
    zsfs: "0",
    qdbz: "0",
    xsfMc: data.nsrmc,
    xsfNsrsbh: data.nsrsbh,
    xsfDzdh: "上海市嘉定区云谷路499号9层901-1室~~021-59556030",
    xsfYhzh: "中国工商银行股份有限公司上海嘉定清河路支行~~1001755809100042390",
    gmfMc: info.name,
    gmfNsrsbh: info.taxNo,
    gmfEmail: info.email,
    gmfMobile: info.gmfMobile,
    kpr: "徐诚",
    sqr: "徐诚",
    bz: info.remark,
    dlzh: "18616658155",
    detailParam: [
      {
        "fphxz": "0",
        "hsbz": "1",
        "spmc": `培训费`,
        "spbm": "3070201020000000000",
        "je": info.amount.toFixed(2),
        "slv": 0.01,
        "lslbs": "0",
      }
    ]
  };

  let resp = await baiwangInvoice({
    data: json_data,
    url: "/cloud/applyInvoice"
  })

  if (resp.code == 500) return cloud.utils.requestReturn({ code: resp.code, msg: resp.msg })

  let query = { ...json_data }
  query["isInvoicing"] = 1
  query["_add_time"] = Date.now()
  if (ctxBody.body.data.hasOwnProperty("invoiceUser")) query["invoiceUser"] = ctxBody.body.data.invoiceUser

  //判断这个是否有过这张发票
  let res = await cloud.nw.db.findByWhereJson({
    dbName,
    whereJson: { ddlsh: query.ddlsh, },
  });
  //如果有这张发票
  if (res && res._id) {

  } else {
    await cloud.nw.db.add({
      dbName, dataJson: query, cancelAddTime: true
    });
  }
  return cloud.utils.requestReturn({ data: resp })
}


let againInvoice = async () => {
  let info = ctxBody.body.data
  const json_data = {
    qyId: info.qyId,
    ddlsh: info.ddlsh,
    fplxdm: info.fplxdm,
    kplx: info.kplx,
    tspz: info.tspz,
    zsfs: info.zsfs,
    qdbz: info.qdbz,
    xsfMc: info.xsfMc,
    xsfNsrsbh: info.xsfNsrsbh,
    xsfDzdh: info.xsfDzdh,
    xsfYhzh: info.xsfYhzh,
    gmfMc: info.gmfMc,
    gmfNsrsbh: info.gmfNsrsbh,
    gmfEmail: info.gmfEmail,
    gmfMobile: info.gmfMobile,
    kpr: info.kpr,
    sqr: info.sqr,
    bz: info.bz,
    dlzh: info.dlzh,
    detailParam: info.detailParam
  };
  let resp = await baiwangInvoice({
    data: json_data,
    url: "/cloud/applyInvoice"
  })
  console.log("重新开票接口", resp);
  if (resp.code == 500) return cloud.utils.requestReturn({ data: resp.msg, code: 500 })
  return cloud.utils.requestReturn({ data: resp })
}